﻿using System;
using MedicWeb.MedicService;
using Microsoft.Reporting.WebForms;

namespace MedicWeb.forms.Reports.Order
{
    public partial class DoctorListReport : SessionPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = Request.QueryString["id"];
            string date = Request.QueryString["date"] ?? "";
            string number = Request.QueryString["number"] ?? "";
            int id = s == null ? 0 : int.Parse(s);
            MedicService.Order order = null;

            Response.ContentType = "application/pdf";

            //var ReportViewer1 = new ReportViewer();

            //ReportViewer1.ProcessingMode = ProcessingMode.Local;
            //ReportViewer1.LocalReport.ReportPath = @"forms\Reports\Order\DoctorListReport.rdlc";

            if (!IsPostBack)
            {
                var ms = new MainService();
                if (id > 0)
                {
                    order = ms.LoadOrder(GetSession(), id);
                }
                if (order == null)
                {
                    throw new Exception("Приказ не сохранён");
                }
                var doctorListByOrder = ms.GetDoctorListByOrder(GetSession(), order);
                if (doctorListByOrder.Length == 0) {
                    doctorListByOrder = new Doctor[]{new Doctor()};
                }
                var sourceDoctors = new ReportDataSource("DoctorList", doctorListByOrder);
                
                ReportViewer1.LocalReport.DataSources.Add(sourceDoctors);
                string d = order.ProtocolDate != null ? order.ProtocolDate.Value.ToShortDateString() : "";
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("data", d));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("number", order.ProtocolNumber ?? ""));
            }

            byte[] render = ReportViewer1.LocalReport.Render("PDF");
            Response.BinaryWrite(render);
            Response.End();
            //ReportViewer1.LocalReport.ReportEmbeddedResource = MapPath("")
        }
    }
}